package com.wlanauth.web.action;

import java.math.BigDecimal;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.wlanauth.core.criteria.GeneralCriteria;
import com.wlanauth.core.criteria.GeneralCriteriaResult;
import com.wlanauth.domain.manage.UserHis;
import com.wlanauth.vo.UserHisVO;
import com.wlanauth.web.core.BaseActionSupport;
import com.wlanauth.web.core.JsonResult;
import com.wlanauth.web.util.RadiusUtils;

/**
 * 计费用户登陆验证
 * @author zz
 * @date 2012-5-9
 * @email charles.zz.life@gmail.com
 */
public class RadiusUserHisAction extends BaseActionSupport {

	private static final long serialVersionUID = 1L;
	
	private UserHisVO entity;//用户实体
	private UserHisVO radiusUserVO;
	/**
	 * 显示
	 * @return
	 */
	public String list(){
		//通用查询
		GeneralCriteria criteria = this.buildWebQueryCriteria(UserHisVO.class);
		
		if(entity!=null){
			if(entity.getUsername()!=null){
				criteria.add(Restrictions.eq("username", entity.getUsername()));
			}
			if(entity.getBindIP()!=null){
				criteria.add(Restrictions.eq("bindIP", entity.getBindIP()));
			}
			if(entity.getBatchId()!=null){
				criteria.add(Restrictions.eq("batchId", entity.getBatchId()));
			}
			
		}
		criteria.addOrder(Order.desc("createTime"));
		GeneralCriteriaResult<UserHisVO> result = criteria.executeQuery();
		this.jsonResult = new JsonResult(this,result.getData(),result.getTotal());
		return this.actionJsonResult();
	}
	public UserHisVO getEntity() {
		return entity;
	}
	public void setEntity(UserHisVO entity) {
		this.entity = entity;
	}
	
	/**
	 * 显示详细信息
	 * @return
	 */
	public String viewDetailPage(){
		UserHis userCondition = new UserHis();
		userCondition.setBindIP(entity.getBindIP());
		
		radiusUserVO = this.getRadiusUserVO(userCondition);
		return "viewDetailPage";
		
	}
	/**
	 * 取用户（含统计流量）
	 * @param user
	 * @return
	 */
	private UserHisVO getRadiusUserVO(UserHis user){
		GeneralCriteria criteria = this.buildWebQueryCriteria(UserHisVO.class);
		criteria.setDataOnly(true);
		if(user.getId()!=null){
			criteria.add(Restrictions.eq("id", user.getId()));
		}
		if(user.getUsername()!=null){
			criteria.add(Restrictions.eq("username", user.getUsername()));
		}
		if(user.getBindIP()!=null){
			criteria.add(Restrictions.eq("bindIP", user.getBindIP()));
		}
		
		GeneralCriteriaResult<UserHisVO> result = criteria.executeQuery();
		if(result.getData()==null||result.getData().size()<1) {
			return null;
		}
		UserHisVO u =  result.getData().get(0);
		processUserVO(u);
		return u;
		
	}

	/**
	 * 将流量数据按系统比率处理
	 * @param radiusUserVO
	 */
	private void processUserVO(UserHisVO radiusUserVO){
		//取系统属性
		
		radiusUserVO.setSumInputOctets(RadiusUtils.getViewOct(radiusUserVO.getSumInputOctets()));
		radiusUserVO.setSumOutputOctets(RadiusUtils.getViewOct(radiusUserVO.getSumOutputOctets()));
		
		BigDecimal sumOctets = RadiusUtils.getViewOct(radiusUserVO.getSumInputOctets().add(radiusUserVO.getSumOutputOctets()));
		
		double ratio = radiusUserVO.getActualFee()/(radiusUserVO.getMaxOctets()*1024);
		
		radiusUserVO.setSumFee(RadiusUtils.getFee(sumOctets.longValue(),ratio));
		
		radiusUserVO.setMaxFee(radiusUserVO.getActualFee());
		
	}
	public UserHisVO getRadiusUserVO() {
		return radiusUserVO;
	}
	public void setRadiusUserVO(UserHisVO radiusUserVO) {
		this.radiusUserVO = radiusUserVO;
	}
	
	
}
